﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Services;
using System.Text.RegularExpressions;

public partial class Pages_Employee_Manage_Stocks_AddStock : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtReceivedDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
           // txtSentDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtDeliveryNumber.Text = "";
        txtReceivedDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        //txtSentDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        ckbStatus.Checked = false;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (Page.IsValid)
        {
            try
            {
                string pinCode = DALAccount.GetOneAccount(Page.User.Identity.Name).PinCode;
                string deliveryNumber = txtDeliveryNumber.Text.Trim();
                string fromStock = ddlFromStock.SelectedValue.TrimEnd(' ');
                string toStock = ddlToStock.SelectedValue.TrimEnd(' ');
                string receivedDate = Convert.ToDateTime(txtReceivedDate.Text).ToString("yyyy-MM-dd");
                string sentDate = "";
                if (txtSentDate.Text == "")
                {
                    sentDate =String.Empty;
                }
                if (ckbStatus.Checked && txtSentDate.Text != "")
                {
                    sentDate = Convert.ToDateTime(txtSentDate.Text).ToString("yyyy-MM-dd");
                }
                if (ckbStatus.Checked && txtSentDate.Text == "")
                {
                    lblMessage.Text = "Sent date must be filled when deliveried to customer";
                    return;
                }
                bool status = ckbStatus.Checked;
                if (DALStock.CheckDeliveryNumber(deliveryNumber))
                {
                    if (!DALStock.CheckDeliverableInStock(deliveryNumber, pinCode))
                    {
                        lblMessage.Text = "Insert Fail: Deliverable 've already exist in stock.";
                        return;
                    }
                    else if (DALStock.AddDeliveralbeInStock(fromStock, toStock, receivedDate, sentDate, status, deliveryNumber, pinCode))
                    {
                        lblMessage.Text = "Insert Successful";
                    }
                    else
                    {
                        lblMessage.Text = "Insert Fail";
                        return;
                    }
                }
                else
                {
                    lblMessage.Text = "Delivery Number doesn't exist. Please check again!";
                    return;
                }
            }
            catch
            {
                lblMessage.Text = "Error In Insert.";
            }
        }
    }

    /// <summary>
    /// This webmethod checks whether an deliverable ID is existed
    /// </summary>
    /// <param name="id">id</param>
    /// <returns>1 if the parameter is an id of an existed deliverable, 0 otherwise, -1 if id string is not a valid id</returns>
    [WebMethod]
    public static int IsExistedID(string id)
    {
        Regex idPattern = new Regex("^[1-9][0-9]*$");
        if (!idPattern.IsMatch(id))
            return -1;
        int n = Int32.Parse(id);
        if (DALDeliverable.GetDeliverablesByDeliverableID(n).Count != 0)
            return 1;
        else
            return 0;
    }
}
